namespace TSL.DAL
{
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Text;
    using TSL.Model;

    public class site
    {
        public void Addsite(TSL.Model.site model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("TSL_site_ADD");
            db.AddInParameter(dbCommand, "id", DbType.AnsiString, model.id);
            db.AddInParameter(dbCommand, "title", DbType.AnsiString, model.title);
            db.AddInParameter(dbCommand, "place", DbType.AnsiString, model.place);
            db.AddInParameter(dbCommand, "context", DbType.AnsiString, model.context);
            db.ExecuteNonQuery(dbCommand);
        }

        public void AddSite(TSL.Model.site model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("TSL_site_ADD");
            db.AddInParameter(dbCommand, "title", DbType.AnsiString, model.title);
            db.AddInParameter(dbCommand, "place", DbType.AnsiString, model.place);
            db.AddInParameter(dbCommand, "context", DbType.AnsiString, model.context);
            db.ExecuteNonQuery(dbCommand);
        }

        public void Delete(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("TSL_Site_Delete");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, ID);
            db.ExecuteNonQuery(dbCommand);
        }

        public bool Exists(string title)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from site");
            strSql.Append(" where title=@title");
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@title", SqlDbType.VarChar, 50) };
            parameters[0].Value = title;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public DataSet GetIndexMenu(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from site");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("order by id");
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public List<TSL.Model.site> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from site ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("order by id asc");
            List<TSL.Model.site> list = new List<TSL.Model.site>();
            using (IDataReader dataReader = DatabaseFactory.CreateDatabase().ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(this.ReaderBind(dataReader));
                }
            }
            return list;
        }

        public DataSet GetListFromView(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from site");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public TSL.Model.site GetModel(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_Site_GetModelByID");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, ID);
            TSL.Model.site model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBind(dataReader);
                }
            }
            return model;
        }

        public DataSet GetSite(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from site");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("order by id");
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public TSL.Model.site ReaderBind(IDataReader dataReader)
        {
            TSL.Model.site model = new TSL.Model.site();
            model.id = Convert.ToInt32(dataReader["id"].ToString());
            model.title = dataReader["title"].ToString();
            model.place = dataReader["place"].ToString();
            model.context = dataReader["context"].ToString();
            model.state = dataReader["state"].ToString();
            return model;
        }

        public void Update(TSL.Model.site model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_Site_Update");
            db.AddInParameter(dbCommand, "id", DbType.Int32, model.id);
            db.AddInParameter(dbCommand, "title", DbType.AnsiString, model.title);
            db.AddInParameter(dbCommand, "place", DbType.AnsiString, model.place);
            db.AddInParameter(dbCommand, "context", DbType.AnsiString, model.context);
            db.AddInParameter(dbCommand, "state", DbType.Int32, model.state);
            db.ExecuteNonQuery(dbCommand);
        }
    }
}

